import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { BusiProvider } from '../../providers/busi/busi';
import { NativeProvider } from '../../providers/native/native';
import { Global } from '../../common/global';
import { isRightSide } from 'ionic-angular/umd/util/util';

/**
 * Generated class for the ProductdetailPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-productdetail',
  templateUrl: 'productdetail.html',
})
export class ProductdetailPage {
  prodid:any;
  productDetail:any;
  showImageUrl:any;
  role = Global.role;
  constructor(public navCtrl: NavController, public navParams: NavParams,public busi:BusiProvider,public native:NativeProvider) {
    this.prodid = this.navParams.get("prodid");
    this.getProdetail();
  }

  ionViewDidEnter(){

  }

  getProdetail(){
    this.busi.getProductDetail(this.prodid).subscribe(
      data=>{
        this.productDetail = data.productdetail;
        this.showImageUrl = Global.base_url + this.productDetail.image_url;
      },
      error=>{}
    );
  }
  ionViewDidLoad() {
    console.log('ionViewDidLoad ProductdetailPage');
  }

  addToShopBus(){
    this.busi.addToShopBus(this.prodid).subscribe(
      data=>{
        this.native.showToast("加入购物车成功！");
      },
      error=>{

      }
    );
  }
  buyNow(){
    this.busi.buyProduct(this.prodid).subscribe(
      data=>{
        this.native.showToast("购买成功！");
        this.navCtrl.pop();
      },
      error=>{

      }
    );
  }
  gotocomments(){
    this.navCtrl.push("CommentaddPage",{prodid:this.prodid});
  }

  gotoemail(){
    this.navCtrl.push("EmailaddPage",{prodid:this.prodid})
  }

  shProd(type){
    const params = {
      prodid:this.prodid,
      type:type
    }
    this.busi.shProduct(params).subscribe(
      data=>{
        this.native.showToast("操作成功！");
        this.navCtrl.pop();
      },
      error=>{

      }
    );
  }

}
